package com.voc.cpnc;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.voc.cpnc.utils.ACache;
import com.voc.cpnc.utils.JsonUtils;
import com.voc.cpnc.utils.LogUtils;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private static final String RECEIVER_ACTION_NMB_GET_WX_OPENID = "com.voc.cpnc.ACTION_NMB_GET_WX_OPENID";
    private static final String RECEIVER_ACTION_NMB_SEND_WX_OPENID = "com.voc.cpnc.ACTION_NMB_SEND_WX_OPENID";

    private static String[] deviceIds = new String[]{"sokuuec6zxda5y6r", "crryp4dyenfc2km6", "j5nbvre6n16xm46d", "hbpjzsc7smtmsegx", "moacvnqqhwf1tuhc", "92kziezegcrxly0f",
            "k4kefjjn3byfslub", "wcdjkohgygcw70fe", "jjxloep0slzhoydw", "0jjtzrojhuby8rvb", "00nqq4wpdlzjodwf", "ntxzwcrxvmkrji7i", "kyn76lywuea3am9b", "hqbycgjgvrfgrzbq",
            "jod7idbiungdrcdt", "otxaipzhj77edahx", "9lkmsr0yzpfobrkz", "hf5wc5g9iw1e5kv8", "ka68muyltpyz6wmo", "m6mplljg8dx7acgf", "zayussjgmecdzgue", "jr2v4258kvqgcamz",
            "0rgsy4x4orqxjo1s", "ghxoctwonuejdw48", "yy6ccrdymc3rpzjn", "lgz4vtydnsordvoj", "cmweebranvglxmop", "liq0rneqazqxc0tt", "zf0ce7kewsod2xjm", "xmub9laoqc9mpfnv",
            "phlslhhzuhwhemzm", "ek4nq411p87cgrl6", "hpmyxzjwj5acnybn", "k3ijcghmycgzvusk", "cseeqefslamjuqsq", "jifspk8j3akih5pq", "ylfbnpmkv5tjyegy", "5kkdgye4ln0gftxk",
            "8u1pr6xqvz6xhn7a", "xpb3tlx5gbq25as2", "fzjcyww9tsfbr1gg", "iwvphviijqwohbkj", "j4zim33fdbcmef8g", "wkhatbgbnamkjcyq", "npqziy4rk8qwdvxt", "upaymujwbw4xxxsv",
            "qfxbgo5yq5hdt21y", "aczocq4y42idvgro", "vvlqceoiorrcqkim", "cdoebuywz7wyuwuz", "cjsob6gwcrdv2npm", "7bt8jthpxjc8jht9", "7lkms2f0sfqli6m5", "mq0zufxdfuvd5l2b",
            "ut5ehxqips50qwp8", "2yyaxwisxufb0vn9", "pdab3686xnub3rfz", "9e3miunirce1kqb1", "m7a7ipkcioriy7b0", "u8gmwjptnaj9krcq", "wptkmq01p2bsorbo", "qkugud0ikifmceca",
            "lb1jmqqloeltoyhh", "gkzjy1yh3yzdxwlw", "mklr9w8kliyntfy8", "6axxdn2ur39co6fc", "ku0em4zyvouixn8v", "8tfhiuxtu6afwmr8", "fgbofwrnohtpyu2n", "1hew3zahmm6lavdx",
            "ffziuakq23o89ebu", "laga65o5ukvtg2cv"};


    private MyBroadcastReceiver receiver = null;

    private ACache aCache;

    private String wxOpenid = null;

    private EditText editContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aCache = ACache.get(this);

        editContent = findViewById(R.id.edit_content);

        receiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVER_ACTION_NMB_SEND_WX_OPENID);
        registerReceiver(receiver, intentFilter);


        Button btnWeixin = findViewById(R.id.bnt_weixin);
        btnWeixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (final String deviceId : deviceIds) {

                            String trsCode = "withdrawZcNos";
                            String p = deviceId;//设备ID

                            final String result = DXSYPlatform.commonConnection(trsCode, p);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    editContent.setText(editContent.getText() + "\n" + result + "," + deviceId);
                                }
                            });
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();


//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        for (String deviceId : deviceIds) {
//                            Intent intent = new Intent();
//                            intent.setAction(RECEIVER_ACTION_NMB_GET_WX_OPENID);
//                            intent.putExtra("deviceId", deviceId);
//                            sendBroadcast(intent);
//                            try {
//                                Thread.sleep(4000);
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                }).start();

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    /**
     * 广播监听
     */
    private class MyBroadcastReceiver extends BroadcastReceiver {

        @SuppressLint("SetTextI18n")
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == null)
                return;
            if (intent.getAction().equals(RECEIVER_ACTION_NMB_SEND_WX_OPENID)) {
                wxOpenid = intent.getStringExtra("wxOpenid");
                final String deviceId = intent.getStringExtra("deviceId");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String trsCode = "bindWxAcctZcNos";
                        final String p = wxOpenid + "\t" + deviceId;//"wxOpenid" + "\t" + "设备ID"
                        final String result = DXSYPlatform.commonConnection(trsCode, p);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (result == null) {
                                    editContent.setText(editContent.getText() + "\n" + "微信绑定失败");
                                } else {
                                    editContent.setText(editContent.getText() + "\n" + result + ",," + wxOpenid + "," + deviceId);
                                }
                            }
                        });
                    }
                }).start();
                aCache.put(deviceId, wxOpenid);
                LogUtils.e(wxOpenid + "," + deviceId);
            }
        }
    }
}
